package com.qike.duola.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.qike.duola.domain.DuolaProject;
import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.DuolaProjectService;
import com.qike.duola.service.DuolaQuoteService;
import com.qike.duola.service.IsysResourceService;
import com.qike.duola.service.IsysUserService;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "报价单控制器")
@CrossOrigin
public class QuoteController {

    private static Logger logger = LoggerFactory.getLogger(QuoteController.class);

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private DuolaQuoteService duolaQuoteService;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加报价单
     *
     * @return
     */
    @PostMapping("/quoteAdd")
    @ApiOperation("添加报价单")
    @ResponseBody
    public BaseResult quoteAdd(@RequestBody QuoteAddDto quoteAddDto) {
        try {
            duolaQuoteService.quoteAdd(quoteAddDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 编辑报价单
     *
     * @return
     */
    @PostMapping("/quoteEdit")
    @ApiOperation("编辑报价单")
    @ResponseBody
    public BaseResult quoteEdit(@RequestBody QuoteEditDto quoteEditDto) {
        try {
            duolaQuoteService.quoteEdit(quoteEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 删除报价单
     *
     * @return
     */
    @PostMapping("/quoteDel")
    @ApiOperation("删除报价单")
    @ResponseBody
    public BaseResult quoteDel(Integer id) {
        try {
            duolaQuoteService.quoteDel(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 报价单列表
     *
     * @return
     */
    @GetMapping("/quoteList")
    @ApiOperation("报价单列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = QuoteListVo.class)
    })
    public BaseResult quoteList(QuoteListDto quoteListDto) {
        try {
            QuoteListVo listVo = duolaQuoteService.quoteList(quoteListDto);
            return RetResponse.makeOKRsp(listVo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 报价单列表
     *
     * @return
     */
    @GetMapping("/quoteById")
    @ApiOperation("报价单通过id获取")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = QuoteListVo.class)
    })
    public BaseResult quoteById(Integer id) {
        try {
            QuoteList listVo = duolaQuoteService.quoteById(id);
            return RetResponse.makeOKRsp(listVo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 通过工程选择对应的报价单
     *
     * @return
     */
    @GetMapping("/quoteListByProjectId")
    @ApiOperation("通过工程选择对应的报价单")
    @ResponseBody
    public BaseResult quoteListByProjectId(Integer projectId) {
        try {
            List<EnumList> listVo = duolaQuoteService.quoteListByProjectId(projectId);
            return RetResponse.makeOKRsp(listVo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 通过报价单id列出所有结算方式
     *
     * @return
     */
    @GetMapping("/payListByQuoteId")
    @ApiOperation("通过报价单id列出所有结算方式")
    @ResponseBody
    public BaseResult payListByQuoteId(Integer quoteId) {
        try {
            List<EnumList> listVo = duolaQuoteService.payListByQuoteId(quoteId);
            return RetResponse.makeOKRsp(listVo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 通过报价单id列出所有商品价格及结算方式
     *
     * @return
     */
    @GetMapping("/payGoodsListByQuoteId")
    @ApiOperation("通过报价单id列出所有商品价格及结算方式")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PayGoodsListByQuoteIdVo.class)
    })
    public BaseResult payGoodsListByQuoteId(Integer quoteId) {
        try {

            List<PayGoodsListByQuoteIdVo> vo = duolaQuoteService.payGoodsListByQuoteId(quoteId);
//            Map<Integer,String> stringMap = new HashMap<>();
//            stringMap.put(1,"123");
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 通过客户id获取所有已审批通过的工程
     *
     * @return
     */
    @GetMapping("/payExaminProjectByUid")
    @ApiOperation("通过客户id获取所有已审批通过的工程")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PayGoodsListByQuoteIdVo.class)
    })
    public BaseResult payExaminProjectByUid(Integer uid,String projectName) {
        try {
//            List<DuolaProject> vo = duolaContractService.GetProjectListByUid(uid);
            List<DuolaProject> vo = duolaQuoteService.payExaminProjectByUid(uid,projectName);
//            Map<Integer,String> stringMap = new HashMap<>();
//            stringMap.put(1,"123");
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
